<template>
  <div class="activity-management">
    <div class="operation-bar">
      <el-button type="primary" @click="handleAdd">新增活动</el-button>
    </div>
    
    <el-table :data="activities">
      <el-table-column prop="activityId" label="活动编号" width="100" />
      <el-table-column prop="title" label="活动标题" />
      <el-table-column prop="maxParticipants" label="参加人数" width="100" />
      <el-table-column prop="startTime" label="开始时间" width="180" />
      <el-table-column prop="endTime" label="结束时间" width="180" />
      <el-table-column prop="status" label="状态" width="100" />
      <el-table-column label="操作" width="200">
        <template #default="scope">
          <el-button type="primary" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="danger" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog 
      v-model="dialogVisible" 
      :title="activityForm.id ? '编辑活动' : '新增活动'"
    >
      <el-form :model="activityForm" label-width="100px">
        <el-form-item label="活动标题">
          <el-input v-model="activityForm.title" />
        </el-form-item>
        <el-form-item label="活动内容">
          <el-input type="textarea" v-model="activityForm.content" />
        </el-form-item>
        <el-form-item label="参加人数">
          <el-input-number v-model="activityForm.maxParticipants" :min="1" />
        </el-form-item>
        <el-form-item label="活动时间">
          <el-date-picker
            v-model="activityForm.activityTime"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
          />
        </el-form-item>
        <el-form-item label="报名时间">
          <el-date-picker
            v-model="activityForm.signupTime"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSubmit">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getActivities, createActivity, updateActivity, deleteActivity } from '@/api/activity'

export default {
  name: 'ActivityManagement',
  data() {
    return {
      activities: [],
      dialogVisible: false,
      activityForm: {
        id: '',
        title: '',
        content: '',
        maxParticipants: 1,
        activityTime: [],
        signupTime: []
      }
    }
  },
  created() {
    console.log('ActivityManagement component created')
    this.fetchActivities()
  },
  methods: {
    async fetchActivities() {
      try {
        const data = await getActivities(this.searchForm)
        this.activities = data
      } catch (error) {
        this.$message.error(error.message)
      }
    },
    handleAdd() {
      this.activityForm = {
        id: '',
        title: '',
        content: '',
        maxParticipants: 1,
        activityTime: [],
        signupTime: []
      }
      this.dialogVisible = true
    },
    handleEdit(activity) {
      this.activityForm = { ...activity }
      this.dialogVisible = true
    },
    async handleDelete(activity) {
      try {
        await this.$confirm('确认删除该活动吗？', '提示', {
          type: 'warning'
        })
        await deleteActivity(activity.id)
        this.$message.success('删除成功')
        this.fetchActivities()
      } catch (error) {
        if (error !== 'cancel') {
          this.$message.error(error.message)
        }
      }
    },
    async handleSubmit() {
      try {
        if (this.activityForm.id) {
          await updateActivity(this.activityForm.id, this.activityForm)
        } else {
          await createActivity(this.activityForm)
        }
        this.dialogVisible = false
        this.$message.success('保存成功')
        this.fetchActivities()
      } catch (error) {
        this.$message.error(error.message)
      }
    }
  }
}
</script> 